import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.HBaseAdmin;

import java.io.IOException;

public class CreateTableDemo {
    static Configuration conf = null;

    static {
        conf = HBaseConfiguration.create();
        conf.set("hbase.rootdir", "hdfs://192.168.126.139:9000/hbase");
        conf.set("hbase.master", "hdfs://192.168.126.139:60010");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("hbase.zookeeper.quorum", "192.168.126.139");
    }

    public static int createTable(String tableName, String[] family) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
        HBaseAdmin admin = new HBaseAdmin(conf);
        HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tableName));
        //HTableDescriptor table = new HTableDescriptor(tableName);
        //HColumnDescriptor 列的相关信息
        for (String str : family) {
            HColumnDescriptor cloumn = new HColumnDescriptor(str);
            //cloumn.setMaxVersions(3);
            table.addFamily(cloumn);
        }
        if (admin.tableExists(tableName)) {
            System.out.println(tableName + "已经存在");
            return -1;
        }
        admin.createTable(table);
        admin.close();
        System.out.println("创建成功");
        return 1;
    }

    public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
        createTable("WangHaiChuan", new String[]{"info", "grade"});
    }
}